﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using DAL;
using BLL.Pictures;

namespace BLL.Store
{
    public class Department : BaseStore
    {
        #region "Constructors"
        public Department(int id, DateTime addedDate, string addedBy, string title, int importance, string description, int parentID, int pictureID)
        {
            this.ID = id;
            this.AddedDate = addedDate;
            this.AddedBy = addedBy;
            this.Title = title;
            this.Importance = importance;
            this.Description = description;
            this.ParentID = parentID;
            this.PictureID = pictureID;
        }
        #endregion

        #region  "Public Properties"
        public string Title { get; set; }

        public int Importance { get; set; }

        public string Description { get; set; }

        public int ParentID { get; set; }

        public int PictureID { get; set; }

        public string FullTitle
        {
            get 
            {
                if (!IsParent)
                    return Title;
                else
                {
                    Department dep1 = GetDepartmentParent(ID);
                    if (!dep1.IsParent)
                        return dep1.Title + " > " + Title;
                    else
                    {
                        Department dep2 = GetDepartmentParent(dep1.ID);
                        return dep2.Title + " > " + dep1.Title + " > " + Title;
                    }
                }
            }
        }

        public bool IsParent
        {
            get 
            {
                Department department = GetDepartmentParent(ID);
                if (department == null)
                    return false;
                else
                    return true;
            }
        }

        public int CountProduct
        {
            get
            {
                return Product.GetProductCountDepartment(this.ID);
            }
        }

        public string ImageUrl
        {
            get 
            {
                return Picture.GetPictureUrl(PictureID, 100, false, null);
            }
        }

        #endregion

        #region Get from DepartmentDetail
        private static Department GetDepartmentFromDepartmentDetails(DepartmentDetails record)
        {
            if (record == null)
                return null;
            else
            {
                return new Department(record.ID, record.AddedDate, record.AddedBy,
                   record.Title, record.Importance, record.Description, record.ParentID, record.PictureID);
            }
        }

        private static List<Department> GetDepartmentListFromDepartmentDetailsList(List<DepartmentDetails> recordset)
        {
            List<Department> departments = new List<Department>();
            foreach (DepartmentDetails record in recordset)
                departments.Add(GetDepartmentFromDepartmentDetails(record));
            return departments;
        }

        #endregion

        #region Static Method

        public static List<Department> ListDepartments()
        {
            return ListDepartments(0, int.MaxValue);
        }
        public static List<Department> ListDepartments(int startRowIndex, int maximumRows)
        {
            List<Department> departments = null;
            string key = "Departments_" + startRowIndex.ToString() + "_" + maximumRows.ToString();

            if (BaseStore.Settings.EnableCaching && BizObject.Cache[key] != null)
            {
                departments = (List<Department>)BizObject.Cache[key];
            }
            else
            {
                List<DepartmentDetails> recordset = SiteProvider.Store.ListDepartments(GetPageIndex(startRowIndex, maximumRows), maximumRows);
                departments = GetDepartmentListFromDepartmentDetailsList(recordset);
                BaseStore.CacheData(key, departments);
            }
            return departments;
        }

        public static int GetDepartmentCount()
        {
            int count = 0;
            string key = "Departments_DepartmentCount";

            if (BaseStore.Settings.EnableCaching && BizObject.Cache[key] != null)
            {
                count = (int)BizObject.Cache[key];
            }
            else
            {
                count = SiteProvider.Store.GetDepartmentsCount();
                BaseStore.CacheData(key, count);
            }
            return count;
        }

        public static List<Department> ListDepartmentsParent()
        {
            List<Department> departments = null;
            string key = "DepartmentsParent";

            if (BaseStore.Settings.EnableCaching && BizObject.Cache[key] != null)
            {
                departments = (List<Department>)BizObject.Cache[key];
            }
            else
            {
                List<DepartmentDetails> recordset = SiteProvider.Store.ListDepartmentsParent();
                departments = GetDepartmentListFromDepartmentDetailsList(recordset);
                BaseStore.CacheData(key, departments);
            }
            return departments;
        }

        public static List<Department> ListDepartmentsByParentID(int ParentID)
        {
            if (ParentID == 0)
                return ListDepartments(0, int.MaxValue);
            else
            {
                List<Department> departments = null;
                string key = "Departments_By_ParentID_" + ParentID.ToString();

                if (BaseStore.Settings.EnableCaching && BizObject.Cache[key] != null)
                {
                    departments = (List<Department>)BizObject.Cache[key];
                }
                else
                {
                    List<DepartmentDetails> recordset = SiteProvider.Store.ListDepartments_By_ParentID(ParentID);
                    departments = GetDepartmentListFromDepartmentDetailsList(recordset);
                    BaseStore.CacheData(key, departments);
                }
                return departments;
            }
        }

        public static Department GetDepartmentByID(int departmentID)
        {
            Department department = null;
            string key = "Department_" + departmentID.ToString();

            if (BaseStore.Settings.EnableCaching && BizObject.Cache[key] != null)
            {
                department = (Department)BizObject.Cache[key];
            }
            else
            {
                department = GetDepartmentFromDepartmentDetails(SiteProvider.Store.GetDepartmentByID(departmentID));
                BaseStore.CacheData(key, department);
            }
            return department;
        }

        public static bool UpdateDepartment(int id, string title, int importance, string description, int parentID, int pictureId)
        {
            DepartmentDetails record = new DepartmentDetails(id, DateTime.Now, BizObject.CurrentUserName, title, importance, description, parentID, pictureId);
            bool ret = SiteProvider.Store.UpdateDepartment(record);
            BizObject.PurgeCacheItems("Department");
            return ret;
        }

        public static bool DeleteDepartment(int id)
        {
            bool ret = SiteProvider.Store.DeleteDepartment(id);
            BizObject.PurgeCacheItems("Department");
            return ret;
        }

        public static int InsertDepartment(string title, int importance, string description, int parentID, int pictureId)
        {
            DepartmentDetails record = new DepartmentDetails(0, DateTime.Now, BizObject.CurrentUserName, title, importance, description, parentID, pictureId);
            int ret = SiteProvider.Store.InsertDepartment(record);
            BizObject.PurgeCacheItems("Department");
            return ret;
        }

        public static int GetDepartmentsCountByParentID(int parentID)
        {
            int countDep = 0;
            string key = "Department_DepartmentCount_ParentID_" + parentID.ToString();

            if (BaseStore.Settings.EnableCaching && BizObject.Cache[key] != null)
            {
                countDep = (int)BizObject.Cache[key];
            }
            else
            {
                countDep = SiteProvider.Store.DepartmentsGet_Count_By_ParentID(parentID);
                BaseStore.CacheData(key, countDep);
            }
            return countDep;
        }

        public static int GetDepartmentIDByProductID(int productID)
        {
            int departmentID = 0;
            string key = "GetDepartmentIDByProductID_" + productID.ToString();

            if (BaseStore.Settings.EnableCaching && BizObject.Cache[key] != null)
            {
                departmentID = (int)BizObject.Cache[key];
            }
            else
            {
                departmentID = SiteProvider.Store.GetProductByID(productID).DepartmentID;
                BaseStore.CacheData(key, departmentID);
            }
            return departmentID;
        }

        public static Department GetDepartmentParent(int id)
        {
            Department department = null;
            string key = "DepartmentParent_" + id.ToString();

            if (BaseStore.Settings.EnableCaching && BizObject.Cache[key] != null)
            {
                department = (Department)BizObject.Cache[key];
            }
            else
            {
                department = GetDepartmentFromDepartmentDetails(SiteProvider.Store.DepartmentsGetParent(id));
                BaseStore.CacheData(key, department);
            }
            return department;
        }

        #endregion

        #region Method
        public bool Delete()
        {
            bool success = Department.DeleteDepartment(this.ID);
            if (success)
                this.ID = 0;
            return success;
        }

        public int Insert()
        {
            return Department.InsertDepartment(this.Title, this.Importance, this.Description, this.ParentID, this.PictureID);
        }

        public bool Update()
        {
            return Department.UpdateDepartment(this.ID, this.Title, this.Importance, this.Description, this.ParentID, this.PictureID);
        }

        #endregion
    }
}
